//
//  HomeViewController.swift
//  WeiBo
//
//  Created by Gao on 16/3/1.
//  Copyright © 2016年 Gao. All rights reserved.
//

import UIKit
import AFNetworking
class HomeViewController: BaseViewController
{
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
       AFHTTPSessionManager()
        if !isLogin
        {
             configLoginView(true, imageNamed: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        setUpNav()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changTitleArrowDirection", name:AnimationWillShowOrWillDimiss , object: nil)
    }
    
    deinit
    {
         NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //导航条的配置
    func setUpNav()
    {
        
        //1.左右两边的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_friendattention",target: self, action: "leftBarbuttonItemClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_pop",target: self, action: "rightBarbuttonItemClick")
        
        //2.中间的标题
        let titleButton = TitleButton()
            titleButton.setTitle("用户名字", forState: .Normal)
            titleButton.addTarget(self, action: "titleButtonClick:", forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleButton
        
    }
    //改变标题尖头的方向
    func changTitleArrowDirection()
    {
        let button = navigationItem.titleView as! UIButton
        button.selected = !button.selected
    }
    
    //标题的点击
    func titleButtonClick(button:UIButton)
    {
        //1.转场动画的设置
        let storyBoard = UIStoryboard(name: "PopoverViewController", bundle: nil)
        //1.1转场动画的实例
        let PopoverController = storyBoard.instantiateInitialViewController()
        //1.2设置样式为自定义
            PopoverController?.modalPresentationStyle = UIModalPresentationStyle.Custom
        //1.3设置代理
            PopoverController?.transitioningDelegate = popoverAnimationManager
        
        presentViewController(PopoverController!, animated: true, completion: nil)
    }
    
    func leftBarbuttonItemClick()
    {
        print(__FUNCTION__)
    }
    
    func rightBarbuttonItemClick()
    {
         let QRCodeStoryBoard = UIStoryboard(name: "QRCodeController", bundle: nil)
         let QRController = QRCodeStoryBoard.instantiateInitialViewController()!
         presentViewController(QRController, animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    lazy var popoverAnimationManager:PopoverAnimationManager =
    {
        let animation = PopoverAnimationManager()
            //设置弹出动画的位置和大小
            animation.presentFrame = CGRect(x: 100, y: 56, width: 200, height: 200)
        return animation
    }()

}
